<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@include file="/pages/hy/common.jsp"%>
<%-- <%@include file="/pages/jyy/jsandcss.jsp" %> --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>排产单</title>
<script type="text/javascript">
$(function () {
    //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();

    //2.初始化Button的点击事件
    var oButtonInit = new ButtonInit();
    oButtonInit.Init();
  	//jQuery，既阻止默认行为又停止冒泡
    $("#button").on('click',function(){
    	    return false;//当然 也阻止了事件本身
    });
    layui.use([ 'laydate' ], function() {
		var laydate = layui.laydate;
	});
    //时间控件
    $(".form_datetime").datetimepicker({
	 format: "yyyy-mm-dd",
	 autoclose: true,
	 todayBtn: true,
	 todayHighlight: true,
	 showMeridian: true,
	 pickerPosition: "bottom-left",
	 language: 'zh-CN',//中文，需要引用zh-CN.js包
	 startView: 2,//月视图
	 minView: 2//日期时间选择器所能够提供的最精确的时间选择视图
	 });
    
    
    //查询车间信息
    $.ajax({
        url:'${pageContext.request.contextPath }/jy/selectCarroom.do',
        type:"POST",
        dataType:'json',
        success:function (result) {
            for (var i = 0; i < result.length; i++) {
                $("#carroom").append("<option value="+result[i].id+">"+result[i].name+"</option>");
            }
        }
    });
    
    
    //带条件查询
    $("#btn_query").click(function(){
    	 $('#tb_departments').bootstrapTable('refresh');
    });
	
    //去添加页面
    $("#btn_add").click(function(){
        window.location.href="${pageContext.request.contextPath }/jy/goCreatedayshedu.do";
    });
    
    //去修改页面
    $("#btn_edit").click(function(){
    	var a= $('#tb_departments').bootstrapTable('getSelections');  
    	if(a.length == 1) {
    		//判断计划单的状态，"未提交/审核未通过"
    		if(a[0].remark1 == "未提交" ||a[0].remark1 == "审核未通过"  ) {
    			 window.location.href="${pageContext.request.contextPath }/jy/goEditdayshedu.do?id="+a[0].id;
    		}else {
    			toastr.warning('对不起，此计划单不能修改');
    			return;
    		}
    	}else {
    		toastr.warning('请选择一条数据');
    	}
    });
    
    //删除
    $("#btn_delete").click(function(){
    	var a= $('#tb_departments').bootstrapTable('getSelections'); 
    	
    	if (a.length <= 0) {
    		toastr.warning('请至少选择一条数据');
    	} else {
    		 var ids = "";
    	        for(var i = 0;i<a.length;i++) {
    	        	  //判断计划单的状态，"未提交/审核未通过"
    				if(a[i].remark1 == "未提交" ||a[i].remark1 == "审核未通过" ) {
    					
    				}else {
    					toastr.warning('对不起，计划单中存在不能删除的记录');
    					return;
    				}
    	       	 if(i != a.length-1) {
    	       	 	ids +=a[i].id+",";
    	       	 }else {
    	       	 	ids +=a[i].id;
    	       	 }
    	        }
    	
    		//询问框
			Ewin.confirm({message : "确认要删除选择的数据吗？"}).on(
					function(e) {
						if (!e) {return;}
						$.ajax({
				   	        type: "POST",
				   	        url: "${pageContext.request.contextPath }/jy/deleteDayshedu.do",
				   	        data: {ids:ids},
				   	        dataType: "json",
				   	        success: function(data){
				   	        	if(data >0) {
				   	        		  toastr.success("删除成功");    	        		
				   	        		  $('#tb_departments').bootstrapTable('refresh');
				   	        		  $('#tb_departments2').bootstrapTable('refresh');
				   	        	}
				   	        }
			   	    })
			} );
    	}
    });
    
    //导出当前页
    $("#btn_export").click(function(){
        window.location.href="${pageContext.request.contextPath }/jy/exportDayshedu.do";
    });
    
    //提交审批
    $("#btn_subsp").click(function(){
    	var a= $('#tb_departments').bootstrapTable('getSelections');  
    	if(a.length == 1){
	    	if(a[0].remark1 != "未提交" && a[0].remark1 != "审批未通过") {
	    		toastr.warning('请选择未提交或审批未通过的信息');
	    		return;
	    	}else{ 
	    		$('#myModal').modal(open);
	    		$("#did").val(a[0].id)
	    		$("#number").val(a[0].dayshedunum)
	    		$("#type").val("生产排产")
	    		$("#staffs").val("")
	    		$("#staffids").val("")
	    	}
    	}else{
    		toastr.warning('请选择一未审批条信息');
    	}
    	/* var a= $('#tb_departments').bootstrapTable('getSelections'); 
    	if(a[0].remark1 == "审批完成") {
    		toastr.warning('审批已完成');
    		return;
    	}
    	
        var ids = "";
        for(var i = 0;i<a.length;i++) {
       	 if(i != a.length-1) {
       	 	ids +=a[i].id+",";
       	 }else {
       	 	ids +=a[i].id;
       	 }
        }
    		$.ajax({
    	        type: "POST",
    	        url: "${pageContext.request.contextPath }/jy/startApply2.do",
    	        data: {ids:ids},
    	        dataType: "json",
    	        success: function(data){
    	        	if(data == 1) {
    					toastr.success("审批成功");
    	        		  $('#tb_departments').bootstrapTable('refresh');
    	        	}
    	        }
    	    }); */ 
    });
    
    //撤销审批
    $("#btn_cxsp").click(function(){
    	var a= $('#tb_departments').bootstrapTable('getSelections');  
    	if(a[0].remark1 != "待审批") {
    		toastr.warning('请选择待审批的信息');
    		return;
    	}else{
    		//询问框
    		Ewin.confirm({message : "确认要撤销该数据的审批吗？"}).on(function(e) {
   				if (!e) {return;}
	    		$.ajax({
	       	        type: "POST",
	       	        url: "${pageContext.request.contextPath }/approve/revoke.do",
	       	        data: {aid:a[0].remark2},
	       	        dataType: "json",
	       	        success: function(data){
	      	        	if (data > 0) {
							toastr.success('撤销成功');
							setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
								$('#tb_departments').bootstrapTable('refresh');  
							},1000);
						} else {
							toastr.error('撤销失败');
						}
	       	        }
	       	    });
    		});
    	}
    });
  //单击
    $('#staffTable').on('click-row.bs.table',function (e, row, element) {
        	//alert(row.staffid);
        	var staffs = $.trim($('#staffs').val());
        	var staffids = $.trim($('#staffids').val());
        	if(staffids == ""){
        		$("#staffids").val(row.staffid)
        	}else{
        		$("#staffids").val(staffids+","+row.staffid)
        	}
        	if(staffs == ""){
        		$("#staffs").val(row.staffname)
        	}else{
        		$("#staffs").val(staffs+"——>"+row.staffname)
        	}
        	
    });
    //作业关闭
    $("#btn_shudown").click(function(){
    	var a= $('#tb_departments2').bootstrapTable('getSelections'); 
    	if (a.length <= 0) {
    		toastr.warning('请选择一条数据');
    	} else {
    		$.ajax({
    	        type: "POST",
    	        url: "${pageContext.request.contextPath }/jy/shutdwonDayshedudetail.do",
    	        data: {id:a[0].id},
    	        dataType: "json",
    	        success: function(data){
    	        	if(data == 1) {
    	        		toastr.success("关闭成功"); 
    	        		  $('#tb_departments2').bootstrapTable('refresh');
    	        	}
    	        }
    	    }); 
    	}
    });
    
});

var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $('#tb_departments').bootstrapTable({
            url: '${pageContext.request.contextPath }/jy/selectDayshedu.do',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            queryParams: oTableInit.queryParams,//传递参数（*）
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 20, 30],        //可供选择的每页的行数（*）
            clickToSelect: true,                //是否启用点击选中行
            height: 400,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
            detailView: false,                   //是否显示父子表
            
            columns:  [{
                checkbox: true
            },{
                field: 'carroom',
                title: '车间'
            }, {
                field: 'remark3',
                title: '注塑机吨位'
            }, {
                field: 'equipment',
                title: '注塑机编号'
            }, {
                field: 'clazz',
                title: '班次',
                formatter: function (value, row, index) {  
						if(value == 1) {
							return "白班";
						}else {
							return "夜班";
						}
                }  
            }, {
                field: 'dayshedunum',
                title: '排产单号'
            }, {
            	field: 'plannum',
                title: '计划单号'
            }, {
            	field: 'remark1',
                title: '审批状态'
            }],
            onCheck: function (row) {
            	$('#tb_departments2').bootstrapTable(  
                        "refresh",  
                        {  
                      	  url:'${pageContext.request.contextPath }/jy/selectDaysheduDetail.do?dayshedunum='+row.dayshedunum+'&equipment='+row.equipment
                        }  
               	 ); 
            }, 
        });
        
        $('#tb_departments2').bootstrapTable({
        	 method: 'get',                      //请求方式（*）
             singleSelect : true,                // 单选checkbox 
             striped: true,                      //是否显示行间隔色
             cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
             pagination: true,                   //是否显示分页（*）
             sortable: false,                     //是否启用排序
             sortOrder: "asc",                   //排序方式
             sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
             pageNumber:1,                       //初始化加载第一页，默认第一页
             pageSize: 10,                       //每页的记录行数（*）
             pageList: [10, 20, 30],        //可供选择的每页的行数（*）
             clickToSelect: true,                //是否启用点击选中行
             height: 300,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
             uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
             detailView: false,                   //是否显示父子表
            columns:  [{
                checkbox: true
            }, {
                field: 'remark3',
                title: '注塑机编号'
            }, {
                field: 'cartype',
                title: '车种'
            }, {
                field: 'pnum',
                title: '品编'
            }, {
                field: 'pname',
                title: '品名'
            }, {
                field: 'remark4',
                title: '计划完工日期'
            }, {
                field: 'plannum',
                title: '排产数量'
            }, {
                field: 'remark7',
                title: '当前完成数量'
            }, {
                field: 'remark2',
                title: '剩余数量'
            } , {
                field: 'state',
                title: '状态'
            } ]
        }); 
        
        
        $('#staffTable').bootstrapTable({
            url: '${pageContext.request.contextPath }/staff/list.do',         //请求后台的URL（*）
            showExport: true, //是否显示导出
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            queryParams: function (params) {
                var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                        limit: params.limit,   //页面大小
                        offset: params.offset,  //页码
                        staffname:$.trim($('#staffnameTJ').val()), 
                		departmentname:$.trim($('#departmentnameTJ').val()),  
                    };
                    return temp;
                },//传递参数（*）
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList : [10,20,30],       //可供选择的每页的行数（*）
            strictSearch: true,
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            height: 413,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "staffid",                     //每一行的唯一标识，一般为主键列
            detailView: false,                   //是否显示父子表
            columns: [{
                field: 'staffid',
                title: '员工id',
                visible: false
            }, {
                field: 'staffnumbering',
                title: '员工编号'
            }, {
                field: 'staffname',
                title: '员工姓名'
            }, {
                field: 'phone',
                title: '电话'
            }, {
                field: 'department.departmentname',
                title: '部门'
            }, {
                field: 'stremark1',
                title: '职位'
            }, {
                field: 'role.rname',
                title: '角色'
            }],
        });
        
    };
    
    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码
            datatime: $("#datatime").val(),
            carroom: $("#carroom").val()
        };
        return temp;
    };
    return oTableInit;
};


var ButtonInit = function () {
    var oInit = new Object();
    var postdata = {};

    oInit.Init = function () {
        //初始化页面上面的按钮事件
    };

    return oInit;
};
</script>
</head>
<body>
	<div class="panel-body" style="padding-bottom:0px;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body">
               <form id="formSearch" class="form-horizontal">
                     <div class="form-group">
                     <label for="loginname" class="control-label col-sm-1">日期:</label>
                     <div class="col-sm-3">
                    	 <!-- <input type="date" name="date" id="date" style="width: 85%;"
									placeholder="yyyy-mm-dd" autocomplete="off" class="layui-input"> -->
                     		<input class="form_datetime form-control" type="text" id="datatime" name="datatime"  >
                     		<!-- <input type="text"  id="datatime" name="datatime" placeholder="yyyy-MM-dd"
									autocomplete="on" class="layui-input" onclick="layui.laydate({elem: this})"> -->
                     		
					    </div>
                        <label class="control-label col-sm-1" for="txt_search_departmentname">车间:</label>
                        <div class="col-sm-2">
                            <select class="form-control"  name="carroom" id="carroom">
	                         	<option value="">--请选择--</option>
	                            <%-- <c:forEach var="it" items="${list}">
									<option value="${it.name}">${it.name}</option>
								</c:forEach> --%>
                         </select>
                        </div>
                        <div class="col-sm-1" style="text-align:left;">
                            <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary">查询</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>       

        <div id="toolbar" class="btn-group">
            <button id="btn_add" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            <button id="btn_edit" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
            </button>
            <button id="btn_delete" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
            </button>
            <button id="btn_subsp" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-phone" aria-hidden="true"></span>审批
            </button>
           <!--  <button id="btn_sp" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-pawn" aria-hidden="true"></span>审批
            </button>-->
            <button id="btn_cxsp" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-bishop" aria-hidden="true"></span>撤销审批
            </button> 
            <button id="btn_shudown" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-off" aria-hidden="true"></span>关闭
            </button>
           <!--  <button id="btn_export" type="button" class="btn btn-default">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>导出
            </button> -->
        </div>
        <table id="tb_departments"></table>
           	<br/>
        <table id="tb_departments2"></table>	
    </div>
    
    
    <!-- 审批模态框 -->
	<div class="modal fade bs-example-modal-lg" id="myModal"   tabindex="-1" role="dialog" aria-labelledby="myModal" data-backdrop="static" data-keyboard="false">
	    <div class="modal-dialog modal-lg">
	        <div class="modal-content">
	        
	            <div class="modal-header">
	                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	                <h4 class="modal-title" id="myModalLabel">提交审批</h4>
	            </div>
	            
	            <div class="modal-body">
	            	<form id="approveFrom" class="form-horizontal" style="margin-right: 5%;">
		            	<div class="form-group">
	           				 <label class="col-sm-2 control-label">审批人:</label>
	            			 <div class="col-sm-10"> 
		            			 <input type="text" class="form-control" id="staffs" name="staffs" autocomplete="off" readonly>
		            			 <input type="hidden" id="staffids" name="staffids" >
		            			 <input type="hidden" id="did" name="did" >
		            			 <input type="hidden" id="number" name="number" >
		            			 <input type="hidden" id="type" name="type">
		            			 <span class="xing"></span>
	          				</div>
	          			</div>
          			</form>
	            	<div class="panel panel-default" style="margin-bottom: 0px;">
			            <!-- <div class="panel-heading">查询条件</div>  -->
			            <div class="panel-body" style="padding-bottom: 0px;padding-left: 0px;padding-right: 0px;"> 
			                <form id="formSearch" class="form-horizontal">
			                    <div class="form-group">
			                        <label class="control-label col-sm-2" for="txt_search_departmentname">员工姓名 </label>
			                        <div class="col-sm-3">
			                            <input type="text" class="form-control" id="staffnameTJ">
			                        </div>
			                        <label class="control-label col-sm-1" for="txt_search_departmentname">部门 </label>
			                        <div class="col-sm-3">
			                            <input type="text" class="form-control" id="departmentnameTJ">
			                        </div>
			                        
			                        <div class="col-sm-3" style="text-align:left;">
			                            <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary" onclick="seach()">查询</button>
			                            <script type="text/javascript">
				                            function seach() {
				                            	$('#staffTable').bootstrapTable('refresh'); 
				                            }
			                            </script>
			                        </div>
			                    </div>
			                </form>
			            </div>
			        </div>
	            	<table id="staffTable"></table>
	            </div>
	            
	            <div class="modal-footer">
					<button type="button" class="btn btn-default" 	data-dismiss="modal">关闭</button>
					<button id="button" type="button" class="btn btn-primary" data-dismiss="modal" onclick="approveSave()">提交</button>
					<script type="text/javascript">
					function approveSave() {
					    if ($("#staffs").val() != null && $("#staffs").val() != ""){ 
							var url = "${pageContext.request.contextPath }/approve/insert.do";
							var title="审批提交";
						    $.post(url, $("#approveFrom").serialize(), function(data) {
								if (data > 0) {
									toastr.success(title+'成功');
									setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
										$('#myModal').modal('hide');
										$('#tb_departments').bootstrapTable('refresh');  
									},1000);
								} else {
									toastr.error(title+'失败');
								}
							});
					    }else{
				    		toastr.warning('请选择审批人');
					    } 
					}
					</script>
				</div>
	            
	        </div>
	    </div>
	</div>
</body>
</html>